package cn.tedu.train.management.controller;

import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import cn.tedu.train.management.pojo.entity.Train;
import cn.tedu.train.management.pojo.param.TrainAddNewDTO;
import cn.tedu.train.management.pojo.param.TrainUpdateInfoDTO;
import cn.tedu.train.management.pojo.vo.TrainListItemVO;
import cn.tedu.train.management.pojo.vo.TrainStandardVO;
import cn.tedu.train.management.service.ITrainService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.hibernate.validator.constraints.Range;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/train")
@Api(tags = "列车管理模块")
@Validated
public class TrainController {

   @Autowired
    private ITrainService iTrainService;

    public TrainController() {
        log.debug("创建控制器类对象：TrainController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加列车信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid TrainAddNewDTO trainAddNewDTO) {
        log.debug("开始处理【添加列车信息】的请求，参数：{}", trainAddNewDTO);
        iTrainService.addNew(trainAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除列车信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "列车ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的列车ID值！") Long id) {
        log.debug("开始处理【根据ID删除列车信息】的请求，参数：{}", id);
        iTrainService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改列车信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "列车ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的列车ID值！") Long id,
                                           @Valid TrainUpdateInfoDTO trainUpdateInfoDTO) {
        log.debug("开始处理【修改列车信息】的请求，ID：{}，新数据：{}", id, trainUpdateInfoDTO);
        iTrainService.updateInfoById(id, trainUpdateInfoDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询列车信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "列车ID", required = true, dataType = "long")
    })
    public JsonResult<TrainStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的列车ID值！") Long id) {
        log.debug("开始处理【根据ID查询列车信息】的请求，参数：{}", id);
        TrainStandardVO queryResult = iTrainService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询列车信息列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<TrainListItemVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                      String queryType) {
        log.debug("开始处理【查询列车信息列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<TrainListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = iTrainService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = iTrainService.list(pageNum);
        }
        return JsonResult.ok(pageData);


    }

}
